﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Core.EmailProvider
{
    public class EmailProvider
    {

        /// <summary>
        /// Sends email.
        /// </summary>
        /// <param name="receiver">Email of receiver</param>
        /// <param name="subject">Subject of email.</param>
        /// <param name="body">Body of email</param>
        public static void SendEmail(string receiver, string subject, string body)
        {
            MailMessage msg = new MailMessage();

            msg.To.Add(receiver);
            msg.Subject = subject;
            msg.Body = body;

            SmtpClient smtp = new SmtpClient();

            smtp.Send(msg);
        }


        /// <summary>
        /// Sends the email.
        /// </summary>
        /// <param name="receiver">The receiver.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="senderEmail">The sender email.</param>
        /// <param name="senderName">Name of the sender.</param>
        public static void SendEmail(string receiver, string subject, string body, string senderEmail, string senderName)
        {
            MailMessage msg = new MailMessage();

            msg.To.Add(receiver);
            msg.Subject = subject;
            msg.Body = body;
            msg.From = new MailAddress(senderEmail, senderName);

            SmtpClient smtp = new SmtpClient();

            smtp.Send(msg);
        }

        /// <summary>
        /// Sends emails.
        /// </summary>
        /// <param name="receivers">Emails of receivers</param>
        /// <param name="subject">Subject of email.</param>
        /// <param name="body">Body of email</param>
        public static void SendEmails(List<string> receivers, string subject, string body)
        {
            MailMessage msg = new MailMessage();

            for (int i = 0; i < receivers.Count; i++)
            {
                msg.To.Add(receivers[i]);
            }

            msg.Subject = subject;
            msg.Body = body;

            SmtpClient smtp = new SmtpClient();

            smtp.Send(msg);
        }

        /// <summary>
        /// Sends the emails.
        /// </summary>
        /// <param name="receivers">The receivers.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        public static void SendEmails(IList<User> receivers, string subject, string body)
        {
            if (receivers.Count == 0)
            {
                return;
            }
            MailMessage msg = new MailMessage();

            foreach (User u in receivers)
            {
                msg.To.Add(u.Email);
            }

            msg.Subject = subject;
            msg.Body = body;

            SmtpClient smtp = new SmtpClient();

            smtp.Send(msg);
        }

        public static void SendEmailsThroughGmail(List<string> receivers, string subject, string body)
        {
            MailMessage msg = new MailMessage();

            for (int i = 0; i < receivers.Count; i++)
            {
                msg.To.Add(receivers[i]);
            }

            msg.Subject = subject;
            msg.IsBodyHtml = true;
            msg.Body = string.Format("<html><head></head><body>" + body + "</body>");
            msg.From = new MailAddress("xpeecko@gmail.com");

            //msg.From = new MailAddress("xpeecko@gmail.com");
            //msg.To.Add(new MailAddress("lukas.slivka@gmail.com"));
            //msg.Subject = "This is a test Email subject";
            //msg.IsBodyHtml = true;
            //msg.Body = string.Format("<html><head></head><body><b>Test HTML Email</b></body>");

            SmtpClient client = new SmtpClient();
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true;
            client.Host = "smtp.gmail.com";
            client.Port = 587;

            // setup Smtp authentication
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("xpeecko@gmail.com", "enablelukas");

            client.UseDefaultCredentials = false;
            client.Credentials = credentials;


            try
            {
                client.Send(msg);
            }
            catch (Exception ex)
            {
                throw new Exception("Couldn't send email : " + ex.Message);
            }
            
        }
        /*
        /// <summary>
        /// Filters the users - accordig theis user settings.
        /// </summary>
        /// <param name="receivers">The receivers.</param>
        /// <param name="type">The type of emails.</param>
        /// <returns>Users who want to receive emails of the type</returns>
        public static IList<User> FilterUsers(IList<User> receivers, string type)
        {
            List<User> filteredUsers = new List<User>();

            switch (type)
            {
                case "MailAssignment":
                    {
                        foreach (User u in receivers)
                        {
                            if (DAOUserSettings.GetSettingsForUser(u.ID).MailTaskAssignment)
                            {
                                filteredUsers.Add(u);
                            }
                        }
                        break;
                    }
                case "MailState":
                    {
                        foreach (User u in receivers)
                        {
                            if (DAOUserSettings.GetSettingsForUser(u.ID).MailTaskState)
                            {
                                filteredUsers.Add(u);
                            }
                        }
                        break;
                    }
                default: return receivers;
            }
            return filteredUsers;
        }*/
    }
}
